//
//  XJAllCategoryViewController.m
//  XinJiangMall
//
//  Created by RaoBo on 2018/5/7.
//  Copyright © 2018年 Tzyang. All rights reserved.
//  接口文档说明:http://192.168.31.176:8079/api/v1/goods/category_goods_list
//  接口 查询分类商品接口 http://192.168.31.176:8079/api/v1/goods/category_goods_list


#import "XJAllCategoryViewController.h"
//#import "XJGoodsDetailViewController.h"
#import "RBGoodsDetailViewController.h"

#import "XJAllCategoryCell.h"

#import "XJAllCategoryModel.h"


@interface XJAllCategoryViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>

@property(nonatomic, strong) UICollectionView *collectionView;

@property(nonatomic, strong) NSMutableArray *dataArray;

@end

@implementation XJAllCategoryViewController

#pragma mark - 一 lazy

- (NSMutableArray *)dataArray {
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}


#pragma mark - 二 生命周期

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self createCollectionView];
    
    [self requestShopCategoryList];
}

- (void)createCollectionView {
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    CGFloat w = (KWidth-10)/ 2;
    
    flowLayout.itemSize = CGSizeMake(w, 260);

    // height: 150+35;
//    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, ADAPTHeight(185), KWidth, KHeight-ADAPTHeight(185)) collectionViewLayout:flowLayout];
    
    // 高度始终不对。
    // 150 + 20 + 64
    CGRect tempFrame = self.view.bounds;
    tempFrame.size.height = KHeight - 150 - 50 - kStatusBarAndNavigationBarHeight;
    self.collectionView = [[UICollectionView alloc] initWithFrame:tempFrame collectionViewLayout:flowLayout];

    [self.view addSubview:self.collectionView];
    
    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;
    self.collectionView.backgroundColor = WHITE_COLOR;
    
    [self.collectionView registerClass:[XJAllCategoryCell class] forCellWithReuseIdentifier:@"RecommendCellID"];
}


#pragma mark - UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.dataArray.count;
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    XJAllCategoryCell *item = [collectionView dequeueReusableCellWithReuseIdentifier:@"RecommendCellID" forIndexPath:indexPath];
    
    item.model = self.dataArray[indexPath.row];
    
    return item;
}

#pragma mark - UICollectionViewDelegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    RBGoodsDetailViewController *goodsDetailVC = [RBGoodsDetailViewController new];
    XJAllCategoryModel *model =  self.dataArray[indexPath.row];
    goodsDetailVC.goodsId = model.goodsId;
    
    [self.navigationController pushViewController:goodsDetailVC animated:YES];
}

#pragma mark - item宽高 sbXcode 跳不进去
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    return CGSizeMake((KWidth - 4 )/2, UI_IS_IPHONE5 ? ADAPTHeight(280) : ADAPTHeight(260));
}

#pragma mark - X间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
    return 1;
}
#pragma mark - Y间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
    return 1;
}

#pragma mark - 网络请求。
- (void)requestShopCategoryList{
    NSDictionary *parDic = @{@"page":@"1",
                             @"page_size":@"20",
                             @"shop_id":@"11",
                             @"shop_goods_category_id":self.shopCategoryId,
                             };
    
    
    [XJHttpTool requestShopCategoryDic:parDic successHandler:^(id responseObjc) {
        
//        NSData *jsonData = [NSJSONSerialization dataWithJSONObject:responseObjc options:NSJSONWritingPrettyPrinted error:nil];
//        NSString *jsonStr = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
        
//        XJLog(@"%@",jsonStr);
        
        NSString *code = [NSString stringWithFormat:@"%@",responseObjc[@"code"]];
        if ([code  isEqualToString:@"0"]) {
            
            
            NSArray *category_goods_list = responseObjc[@"data"][@"category_goods_list"];
            
            for (NSDictionary *dic in category_goods_list) {
                XJAllCategoryModel *model = [XJAllCategoryModel mj_objectWithKeyValues:dic];
                [self.dataArray addObject:model];
            }
            
            dispatch_async(dispatch_get_main_queue(), ^{
                [self.collectionView reloadData];
            });
            
        }
    
    } failHandler:^(NSError *error) {
        [JRToast showWithText:[error localizedDescription]];
    }];
}

#pragma mark - 参考返回数据
/**
 
 {
 "data" : {
 "category_goods_list" : [
 {
 "price" : 1,
 "id" : 158,
 "market_price" : 2,
 "name" : "小何店铺",
 "cover" : "http:\/\/static.zertone1.com\/BHRMAHZAZwsqAJAuA6CJs90XmGvJwOWl.jpg",
 "shop_id" : 11
 },
 {
 "price" : 3,
 "id" : 153,
 "market_price" : 3,
 "name" : "1231",
 "cover" : "http:\/\/static.zertone1.com\/W1i6r2QXfH4F9iDnpHwULhgwrtsMbDAT.png",
 "shop_id" : 11
 },
 {
 "price" : 10,
 "id" : 142,
 "market_price" : 20,
 "name" : "iOS测试商品3",
 "cover" : "http:\/\/static.zertone1.com\/c3QNDIRqqim9m3zhtRqpSquiAoTnuH25.png",
 "shop_id" : 11
 },
 {
 "price" : 10,
 "id" : 141,
 "market_price" : 20,
 "name" : "iOS测试商品2",
 "cover" : "http:\/\/static.zertone1.com\/c3QNDIRqqim9m3zhtRqpSquiAoTnuH25.png",
 "shop_id" : 11
 },
 {
 "price" : 10,
 "id" : 140,
 "market_price" : 20,
 "name" : "iOS测试商品1",
 "cover" : "http:\/\/static.zertone1.com\/c3QNDIRqqim9m3zhtRqpSquiAoTnuH25.png",
 "shop_id" : 11
 },
 {
 "price" : 10,
 "id" : 139,
 "market_price" : 20,
 "name" : "1",
 "cover" : "http:\/\/static.zertone1.com\/gAY8K2UkUq0mi83Ra6fBEbhuNuc3ugyA.jpg",
 "shop_id" : 11
 },
 {
 "price" : 10,
 "id" : 138,
 "market_price" : 20,
 "name" : "桃子",
 "cover" : "http:\/\/static.zertone1.com\/gdr2hlulX6bxlfyst5xwYtbKsNVn4ftE.jpg",
 "shop_id" : 11
 },
 {
 "price" : 45,
 "id" : 131,
 "market_price" : 56,
 "name" : "热热回复的",
 "cover" : "http:\/\/static.zertone1.com\/Y5454SbUJCddPB24HpmQw04jYYKRIhkC.jpg",
 "shop_id" : 11
 },
 {
 "price" : 10,
 "id" : 111,
 "market_price" : 11,
 "name" : "西瓜",
 "cover" : "http:\/\/static.zertone1.com\/P2fPAruo0QD3rtM9AC5QvVRewbD51sNz.jpg",
 "shop_id" : 11
 },
 {
 "price" : 1,
 "id" : 103,
 "market_price" : 1,
 "name" : "茄子",
 "cover" : "http:\/\/static.zertone1.com\/Up1PlnGEFc9DuI4KBjS9EeKbaScKONZl.png",
 "shop_id" : 11
 }
 ]
 },
 "code" : 0
 }
 

 */

@end
